package errors

import (
	"errors"
	"fmt"
	"runtime"
	"sieve_engine/lib/config"
	"strconv"
	"strings"
)

var (
	fPosShow = config.Bool("errors.show.file", true, "显示错误文件")
	fPosFull = config.Bool("errors.show.fullpath", false, "显示错误文件完整路径")
)

func New(v ...any) error {
	return errors.New(pos() + strings.TrimSpace(fmt.Sprintln(v...)))
}

func pos() string {
	if !*fPosShow {
		return ""
	}
	_, file, line, ok := runtime.Caller(2)
	if !ok {
		file = "???"
		line = 0
	}
	if !*fPosFull {
		i := strings.LastIndex(file, "/")
		file = file[i+1:]
	}

	return "<" + file + ":" + strconv.Itoa(line) + ">"
}
